<template>
  <CompOperate
      :field="field"
      :designer="designer"
      :design-state="designState"
      :parent-list="parentList"
      :parent-widget="parentWidget"
      :sub-form-row-id="subFormRowId"
      :sub-form-row-index="subFormRowIndex"
      :sub-form-col-index="subFormColIndex"
      :index-of-parent-list="indexOfParentList"
      :display-style="field['options'].displayStyle"
  >
    <div class="scene-pass-widget" :style="getStyle('wrap')">
      <div class="IconLeft" :style="getStyle('IconHead')" v-if="optionsInfo.iconTypeVisit">
        <Icon
            :name="optionsInfo['iconType'] || 'volume'"
        />
      </div>
      <div class="left" :style="getStyle('head')" v-if="headerShown">
        <div v-if="IconIsShown">
          {{ optionsInfo.headerName || '过号' }}
        </div>
      </div>
      <div class="right edit" :key="widgetKey">

        <template v-if="!!passPatientInfos && !!passPatientInfos.length">
          <template v-if="getDataProcessingMode === 1">
            <vue-seamless-scroll
                ref="scroll"
                class="scroll"
                @ScrollEnd="handleEnd"
                :class-option="classOptionTips"
                :data="passPatientInfos"
            >
              <div class="inside-content" :style="getStyle('list')">
                <div
                    class="patient"
                    v-for="(item, index) in passPatientInfos"
                    :key="index"
                >
                  {{ item.orderNo || "" }}&nbsp;{{ item.patientName || "" }}
                </div>
              </div>
            </vue-seamless-scroll>
          </template>

          <template v-if="getDataProcessingMode === 2">
            <div  style="width: 100%">
              <div class="inside-content-slice" :style="getStyle('list')">
                <div
                    class="patient"
                    v-for="(item, index) in passPatientInfos"
                    :key="index"
                >
                  {{ item.orderNo || "" }}&nbsp;{{ item.patientName || "" }}
                </div>
              </div>
            </div>
          </template>
          <template v-if="getDataProcessingMode === 3">
            <div class="inside-content-ellipsis" :style="getStyle('list')">
              <div class="ellipsis-inside">
                <div
                    class="patient"
                    v-for="(item, index) in passPatientInfos"
                    :key="index"
                >
                  {{ item.orderNo || "" }}&nbsp;{{ item.patientName || "" }}
                </div>
              </div>

            </div>
          </template>
        </template>
      </div>
    </div>
  </CompOperate>
</template>

<script>
import {v4 as uuid} from "uuid";
import {pxToVw} from "@/utils/common";
import emitter from "@/utils/designer/emitter";
import vueSeamlessScroll from "vue-seamless-scroll";
import mixin from "@sc/templateDetails/modules/homeMiddle/fieldMixin";
import CompOperate from "@c/public/form-designer/devType/modules/compOperate.vue";
import {Icon} from "vant";

export default {
  name: "scene-pass-widget",

  componentName: "FieldWidget", //必须固定为FieldWidget，用于接收父级组件的broadcast事件

  mixins: [emitter, mixin],

  props: {
    widget: Object,
    field: Object,
    devType: {
      type: Boolean,
      default: false,
    },
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    designer: Object,
    designState: {
      type: Boolean,
      default: false,
    },

    subFormRowIndex: {
      /* 子表单组件行索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormColIndex: {
      /* 子表单组件列索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormRowId: {
      /* 子表单组件行Id，唯一id且不可变 */ type: String,
      default: "",
    },

    headTitle: {
      type: String,
      default: "",
    },
    visibleTitle: {
      type: Boolean,
    },
  },

  components: {
    CompOperate,
    vueSeamlessScroll,
    Icon,
  },

  data() {
    return {
      widgetKey: uuid(),
      optionsInfo: Object(),
    };
  },

  computed: {
    // 标题状态
    headerShown() {
      let {headerShown = true} = this.optionsInfo;
      return headerShown;
    },

    // 过号患者信息
    passPatientInfos() {
      let devType = this.devType;
      let patientList = this.$store.getters["getPassPatientInfo"];
      let {DataProcessingMode = 1, splicelableNumber = 7} = this.optionsInfo;
      let list = devType ? patientList.apiData : patientList.mockData;
      if (parseInt(DataProcessingMode) === 2) {
        return list.slice(0, splicelableNumber);
      }
      return list;
    },

    // 温馨提示的滚动
    classOptionTips() {
      let {scrollableNumber} = this.optionsInfo;
      return {
        step: [1, 1.5, 2, 2.5, 3][0], // 数值越大速度滚动越快
        hoverStop: true,
        limitMoveNum: scrollableNumber, // 开始无缝滚动的数据量 this.dataList.length
        direction: 2, // 0向下 1向上 2向左 3向右
      };
    },

    getStyle() {
      return function (val) {
        const self = this;
        let info = self.optionsInfo || Array();
        let Obj = Object();
        switch (val) {
          case "wrap":
            if (!!info.backgroundColor) {
              Reflect.set(Obj, "backgroundColor", `${info.backgroundColor}`);
            } else {
              Reflect.deleteProperty(Obj, "backgroundColor");
            }
            Reflect.set(Obj, "height", pxToVw(info.contentHeight));
            Reflect.set(
                Obj,
                "margin",
                `${pxToVw(info.marginVertical)} ${pxToVw(info.marginHorizontal)}`
            );
            return Obj;
          case "head":
            if (!!info.backgroundColor) {
              Reflect.set(Obj, "backgroundColor", `${info.backgroundColor}`);
              Reflect.set(Obj, "fontWeight", `${info.headerFontWeight}`);
            } else {
              Reflect.deleteProperty(Obj, "backgroundColor");
            }
            Reflect.set(Obj, "fontSize", pxToVw(info.headerFontSize));
            Reflect.set(Obj, "borderRadius", pxToVw(info.headerBorderRadius));
            Reflect.set(Obj, "color", `${info.headerFontColor}`);
            Reflect.set(
                Obj,
                "backgroundColor",
                `${info.headerBackgroundColor}`
            );
            Reflect.set(
                Obj,
                "paddingLeft",
                pxToVw(info.headerNameWidth)
            );
            Reflect.set(
                Obj,
                "paddingRight",
                pxToVw(info.headerNameWidth)
            );
            Reflect.set(
                Obj,
                "height",
                pxToVw(info.btnHeight)||"auto"
            );
            return Obj;
          case 'IconHead':
            Reflect.set(
                Obj,
                "backgroundColor",
                `${info.IconHeaderBackgroundColor}`
            );
            Reflect.set(
                Obj,
                "paddingLeft",
                pxToVw(info.headerBackgroundColorPaddingWidth)
            );
            Reflect.set(
                Obj,
                "paddingRight",
                pxToVw(info.headerBackgroundColorPaddingWidth)
            );
            Reflect.set(
                Obj,
                "paddingTop",
                pxToVw(info.headerBackgroundColorPaddingHeight)
            );
            Reflect.set(
                Obj,
                "paddingBottom",
                pxToVw(info.headerBackgroundColorPaddingHeight)
            );
            
            return Obj;
          case "list":
            Reflect.set(Obj, "color", `${info.listFontsColor}`);
            Reflect.set(Obj, "fontSize", pxToVw(info.listFontsize));
            Reflect.set(Obj, "fontWeight", `${info.listFontWeight}`);
            return Obj;
        }
      };
    },

    // icn显示控制
    IconIsShown() {
      let {iconVisit = true} = this.optionsInfo
      return iconVisit
    },


    getDataProcessingMode(){
      let {DataProcessingMode = 1} = this.optionsInfo;
      return parseInt(DataProcessingMode)
    }
  },

  created() {
    /* 注意：子组件mounted在父组件created之后、父组件mounted之前触发，故子组件mounted需要用到的prop 需要在父组件created中初始化！！ */
    this.registerToRefList();
    this.initEventHandler();
    this.handleOnCreated();
  },

  mounted() {
    this.handleOnMounted();
  },

  methods: {
    handleEnd() {
      this.widgetKey = uuid();
    },

    // 接收父组件传递过来的数据
    executeMethod(key, data) {
      switch (key) {
        case "passPatientInfo":
          // console.log(`过号-数据处理逻辑-${data}`)
          this.widgetKey = uuid();
          this.$store.dispatch(
              "setPassPatientInfo",
              this.$lodash.cloneDeep(data)
          );
          break;
      }
    },
  },

  watch: {
    // 获取配置项
    field: {
      handler: function (val) {
        let key = val.options.config;
        this.widgetKey = uuid();
        this.optionsInfo = val.options[key];
      },
      deep: true,
      immediate: true,
    },
  },

  destroyed() {
    this.unregisterFromRefList();
  },
};
</script>

<style lang="scss" scoped>
.scene-pass-widget {
  display: flex;
  color: #333;
  overflow: hidden;
  font-size: 32px;
  margin: 10px 28px 16px 28px;
  box-sizing: border-box;
  background: transparent;

  .left {
    white-space: nowrap;
    border-radius: 6px;
    padding: 12px 46px;
    color: white;
    display: flex;
    justify-content: center;
    align-self: center;
    align-items: center;
    background: #ffa72c;
    margin-right: 32px;
    max-width: 118px;
    overflow: hidden;
    flex-shrink: 0;
  }

  .IconLeft {
    white-space: nowrap;
    border-radius: 6px;
    padding: 12px 46px;
    color: white;
    display: flex;
    justify-content: center;
    align-self: center;
    align-items: center;
    background: #ffa72c;
    margin-right: 10px;
    max-width: 118px;
    overflow: hidden;
    flex-shrink: 0;
  }

  .right {
    display: flex;
    flex-direction: row;
    overflow: hidden;
    padding: 8px 18px;
    cursor: pointer;

    .scroll {
      display: flex;
      align-items: center;
      align-self: center;

      ::v-deep > div {
        display: flex;

        .inside-content {
          display: flex;
          font-size: 54px;

          .patient {
            // font-weight: normal;
            white-space: nowrap;
            padding-right: 38px;
          }
        }
      }
    }

    .inside-content-slice {
      height: 100%;
      display: flex;
      align-items: center;
      font-size: 54px;

      .patient {
        // font-weight: normal;
        white-space: nowrap;
        padding-right: 38px;
      }
    }

    .inside-content-ellipsis {
      height: 100%;
      @include singleEllipsis(100%);
      align-items: center;
      font-size: 54px;

      .ellipsis-inside{
        display: flex;
        flex-direction: row;
        height: 100%;
      }

      .patient {
        white-space: nowrap;
        align-self: center;
        padding-right: 38px;
      }
    }
  }
}
</style>
